<?php

/**
 * 注册统计 每个小时1分运行一次 统计用户注册脚本
 *
 * @author zhoujf
 */
require_once dirname(__DIR__) . "/cronBase.php";

use Common\Bridge;

class reg extends cronBase
{

    public function doDefault()
    {
        global $argv;
        //计算昨天的时间
        $time = !empty($argv[1]) ? strtotime($argv[1]) : strtotime('-1 hour');
        $hour = date('Y-m-d 00:00:00', $time);
        $hour1 = date('Y-m-d 23:59:59', $time);
        $type_reg = loadconf('statistics/type')['reg'];

        //用户注册人数
        $select_sql = "SELECT COUNT(*) AS `num` FROM `User` WHERE CreateTime < '" . $hour1 . "' and `CreateTime` >= '" . $hour . "'";
        $reg_num = $this->userQuery($select_sql);

        $rows = ['Type' => $type_reg, 'Num' => intval($reg_num[0]['num']), 'CreateDate' => $hour];

        //写入数据
        $pdo = Bridge::pdo(Bridge::DB_ADMIN_W);
        $pdo->insert('SiteStatus', $rows, ['ignore' => true]);
        $pdo->close();
        Bridge::pdo(Bridge::DB_ADMIN_W, 'force_close');
        $pdo = null;
    }

    /**
     * 查询语句
     *
     * @param string $sql 查询的SQL语句
     * @return array
     */
    private function userQuery($sql)
    {
        $pdo = Bridge::pdo(Bridge::DB_USER_R);
        $obj = $pdo->query($sql);

        return $obj->fetchAll();
    }
}

$app->run();